"""
@author: xxw
@file: 链表反转.py
@time: 2023/5/5 16:12
"""
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def reversed(head: ListNode) -> ListNode:
    prev, cur = None, head
    while cur != None:
        tem = cur.next
        cur.next = prev
        prev = cur
        cur = tem
    return prev

l1 = ListNode(1)
l2 = ListNode(2)
l3 = ListNode(3)
l1.next = l2
l2.next = l3

head = reversed(l1)

while head :
    print(f"{head.val}", end=' ')
    head = head.next
